﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DispatchAssist.BusinessLogical.DAL;
using DispatchAssist.BusinessLogical.Model;
using DispatchAssist.Model;
using CarolLib;

namespace DispatchAssist.BusinessLogical.BLL
{
    public class SystemLogManager
    {
        static log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private static IDataProvider _provider = new SqlDataProvider();
        public static ClientMessageInfo AlertInfoData { set; get; }

        public static void InsertSystemLog(List<SystemLog> logList)
        {
            if (logList == null || logList.Count == 0)
            {
                Logger.Info("无数据需要记录！");
                return;
            }
            try
            {
                _provider.InsertSystemLog(logList);
            }
            catch (Exception e)
            {
                Logger.Error(e);
            }
        }

        public static object QueryLogCommon(string logTypeStr, QueryCondition qc)
        {
            object obj = null;

            try
            {
                if (qc != null)
                {
                    List<QueryCondition> qclist = new List<QueryCondition>();
                    qclist.Add(qc);
                    obj = _provider.QueryLogCommon(logTypeStr, qclist);
                }
                else
                {
                    obj = _provider.QueryLogCommon(logTypeStr, null);


                }
            }
            catch (Exception e)
            {
                Logger.Error(e);

            }
            return obj;
        }

        private static List<NormalAlertInfo> GetAllAlertInfoListByPara(Guid id, int actionFlag)
        {
            //List<NormalAlertInfo> alertList = _provider.GetAllAlertInfo();

            List<NormalAlertInfo> result = new List<NormalAlertInfo>();

            //Guid[] ids = alertList.Distinct(new Comparint()).ToDictionary<NormalAlertInfo, Guid>(f => f.AlertID).Keys.ToArray();

            Guid[] ids = new Guid[1];
            ids[0] = id;
            List<NormalAlertInfo> alertList = _provider.GetNormalAlertInfoByIDs(ids);
            List<AlertAllInfo> infoList = _provider.GetAlertHisInfoByIDs(ids);


            //foreach (NormalAlertInfo info in alertList)
            //{
            //List<AlertAllInfo> aainfo = infoList.Where(f => f.AlertID == info.AlertID).ToList<AlertAllInfo>();

            //List<NormalAlertInfo> newList = ClientInfoCacheHelper.SplitNormalAlertModel(alertList[0], infoList[0]);
            if (alertList != null && infoList.Count > 0)
            {
                List<NormalAlertInfo> newList = GetNormalAlertInfoListOfMessageBody(alertList[0], infoList[0], actionFlag);



                result.AddRange(newList);
            }
            // }

            return result;


            // return alertList;
        }

        private static List<NormalAlertInfo> GetNormalAlertInfoListOfMessageBody(NormalAlertInfo model, AlertAllInfo source, int action)
        {

            List<NormalAlertInfo> result = new List<NormalAlertInfo>();
            if (action == (int)Enums.AlertActionFlag.ConfirmAcars)
            {
                action = (int)(Enums.AlertActionFlag.AutoSendAcarsAlert | Enums.AlertActionFlag.ConfirmAcars);
            }


            try
            {
                string targetType = "";

                if (action == (int)Enums.AlertActionFlag.ManualSendAcrarsAlert || action == (int)(Enums.AlertActionFlag.AutoSendAcarsAlert | Enums.AlertActionFlag.ConfirmAcars))
                {
                    targetType = CommonCacheData.CacheACARDevice.GetACARSDeviceTypeByPara(model.TailNo);
                }

                string template = CommonCacheData.CacheTemplate.GetTemplateContentByPara(action, targetType);

                string message;

                try
                {
                    message = MessageBodyFactory.GetMessageBody<AlertAllInfo>(source, template);
                }
                catch
                {
                    message = "";
                }

                NormalAlertInfo normalAlert = model.CopyModel();
                normalAlert.AlertActionFlag = action;
                normalAlert.MessageBody = message;

                result.Add(normalAlert);
            }
            catch (Exception e)
            {
                Logger.Error("error in SplitNormalAlertModel action:", e);
            }


            return result;
        }
        private static List<SMSAlertAllInfo> GetAllSMSAlertInfoListByPara(Guid id, int action)
        {
            Guid[] ids = new Guid[1];
            ids[0] = id;
            List<SMSAlertAllInfo> smsList = _provider.GetSMSAlertInfoByIDs(ids);
            //List<SMSAlertAllInfo> smsList = _provider.GetRecentClientSMSAlertInfo();

            List<SMSAlertAllInfo> result = new List<SMSAlertAllInfo>();
            if (smsList != null && smsList.Count > 0)
            {
                foreach (SMSAlertAllInfo info in smsList)
                {

                    //List<SMSAlertAllInfo> newList = ClientInfoCacheHelper.SplitSMSAlertModel(info);
                    List<SMSAlertAllInfo> newList = GetSMSAlertInfoListOfMessageBody(info, action);

                    result.AddRange(newList);
                }
            }
            return result;
            //return alertList;
        }

        private static List<SMSAlertAllInfo> GetSMSAlertInfoListOfMessageBody(SMSAlertAllInfo model, int action)
        {
            List<SMSAlertAllInfo> result = new List<SMSAlertAllInfo>();
            if (action == (int)Enums.AlertActionFlag.ConfirmSMS)
            {
                action = (int)(Enums.AlertActionFlag.AutoSendSMSAlert | Enums.AlertActionFlag.ConfirmSMS);
            }

            string template = CommonCacheData.CacheTemplate.GetTemplateContentByPara(action, null);

            if (string.IsNullOrEmpty(template))
            {
                Logger.Error("no template info ,action:" + action.ToString());
            }

            try
            {
                string message = MessageBodyFactory.GetMessageBody<SMSAlertAllInfo>(model, template);

                SMSAlertAllInfo info = model.CopyModel();
                info.AlertActionFlag = action;
                info.MessageBody = message;

                result.Add(info);
            }
            catch (Exception e)
            {
                Logger.Error(e);
            }
            return result;

        }


        public static void GetNewAlertInfoData(Guid id, int action)
        {
            try
            {
                if (AlertInfoData == null)
                {
                    AlertInfoData = new ClientMessageInfo();
                }
                List<SMSAlertAllInfo> smsList = null;
                List<NormalAlertInfo> alertList = null;
                if (action == (int)Enums.AlertActionFlag.ConfirmSMS || action == (int)Enums.AlertActionFlag.ManualSendSMSAlert)
                {
                    smsList = GetAllSMSAlertInfoListByPara(id, action);
                }
                else
                {
                    alertList = GetAllAlertInfoListByPara(id, action);
                }




                if (alertList == null)
                {
                    AlertInfoData.NormalAlertList = new List<NormalAlertInfo>();
                }
                else
                {
                    AlertInfoData.NormalAlertList = alertList;
                }

                if (smsList == null)
                {
                    AlertInfoData.SMSAlertList = new List<SMSAlertAllInfo>();
                }
                else
                {
                    AlertInfoData.SMSAlertList = smsList;
                }
            }
            catch (Exception e)
            {
                Logger.Error("get cache data from DB error", e);
            }

        }
    }
}
